package csu.web.mypetstore.web.servlet;

import csu.web.mypetstore.domain.Account;
import csu.web.mypetstore.domain.Cart;
import csu.web.mypetstore.service.LogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


public class CartFormServlet extends HttpServlet {

    private static final String CART_FORM = "/WEB-INF/jsp/cart/cart.jsp";

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        Cart cart = (Cart) session.getAttribute("cart");

        if (cart == null){
            cart = new Cart();
            session.setAttribute("cart",cart);
        }

        Account account = (Account) session.getAttribute("loginAccount");
//        if(account != null) {
//            String strUrl = "http://" + req.getServerName() + ":" + req.getServerPort()
//                    + req.getContextPath() + req.getServletPath() + "?" + req.getQueryString();
//            LogService logService = new LogService();
//            String logInfo = logService.logInfo(" ") +"用户" +account.getUsername()+  "  " + "访问购物车页面";
//            logService.insertLogInfo(account.getUsername(),logInfo);
//        }

        req.getRequestDispatcher(CART_FORM).forward(req,resp);
    }
}
